package cn.tedu.ivos.dictoption.servie.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dictoption.mapper.DictOptionMapper;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionSaveParam;
import cn.tedu.ivos.dictoption.pojo.entity.DictOption;
import cn.tedu.ivos.dictoption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictoption.servie.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageReader;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class DictOptionServiceImpl implements DictOptionService {

    @Autowired
    private DictOptionMapper mapper;

    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<DictOptionVO> select(DictOptionQuery dictOptionQuery) {
        return mapper.select(dictOptionQuery);
    }

    @Override
    public void save(DictOptionSaveParam dictOptionSaveParam) {
        DictOption dictOption = new DictOption();
        BeanUtils.copyProperties(dictOptionSaveParam, dictOption);
        if (dictOption.getId() == null) {
            System.out.println("新增操作");
            dictOption.setCreateTime(new Date());
            mapper.save(dictOption);
        } else {
            System.out.println("更新操作");
            dictOption.setUpdateTime(new Date());
            mapper.update(dictOption);
        }
    }

    @Override
    public void del(Long id) {
        mapper.del(id);
    }

    @Override
    public List<DictOptionVO> selectDictOptionByDictCode(String code) {
//        最好是一层一层查 避免外表查询 效率会变慢
        /*
//        自己的写法
        Long id =  mapper.selectIdByCode(code);
        List<DictOptionVO> list  = mapper.selectDictOptionByDictDictId(id);
        log.debug("list :"+list);
        return list;
         */

//        老师的写法
        DictQuery query = new DictQuery();
        query.setCode(code);
        List<DictVO> dictVOList = dictMapper.select(query);
        if (dictVOList.size()>0 && dictVOList != null)
        {
            DictVO dictVO = dictVOList.get(0);
            DictOptionQuery dictOptionQuery = new DictOptionQuery();
            dictOptionQuery.setDictId(dictVO.getId());
            List<DictOptionVO> listDictOptionVO = mapper.selectDictOptionByDictDictId(dictOptionQuery);
            return listDictOptionVO;
        }else{
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }


    }
}
